package com.macro.mall.pms.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.exception.BusinessException;
import com.macro.mall.pms.domain.Sku;
import com.macro.mall.pms.entity.SkuEntity;
import com.macro.mall.pms.mapper.SkuMapper;
import com.macro.mall.pms.service.SkuService;
import com.macro.mall.pms.service.bo.InsertSkuInput;
import com.macro.mall.pms.service.bo.PagedQuerySkuInput;
import com.macro.mall.pms.service.bo.UpdateSkuInput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * skuService
 *
 * @author xuyanjun
 * @date 2019-12-13
 */
@Slf4j(topic = "SkuServiceImpl")
@Service
public class SkuServiceImpl implements SkuService {

    @Autowired
    private SkuMapper skuMapper;

    /**
     * 新增sku
     *
     * @param input input
     * @return 新增id
     */
    @Override
    public long insert(InsertSkuInput input) {
        SkuEntity skuEntity = new SkuEntity();
        skuEntity.setProductId(input.getProductId())
                .setSkuCode(input.getSkuCode())
                .setPrice(input.getPrice())
                .setStock(input.getStock())
                .setLowStock(input.getLowStock())
                .setSp1(input.getSp1())
                .setSp2(input.getSp2())
                .setSp3(input.getSp3())
                .setPic(input.getPic())
                .setSale(input.getSale())
                .setPromotionPrice(input.getPromotionPrice())
                .setLockStock(input.getLockStock());

        skuMapper.insert(skuEntity);
        return skuEntity.getId();
    }

    /**
     * 递增销量
     *
     * @param productId     商品id
     * @param increaseCount 递增数量
     */
    @Override
    public void increaseSale(long productId, int increaseCount) {
        skuMapper.increaseSale(productId, increaseCount);
    }

    /**
     * 修改sku
     *
     * @param input input
     */
    @Override
    public void update(UpdateSkuInput input) {
        SkuEntity skuEntity = skuMapper.selectById(input.getId());
        if (skuEntity == null) {
            throw new BusinessException(String.format("未能根据Id:%s找到指定的sku。", input.getId()));
        }

        skuEntity = new SkuEntity();
        skuEntity
                .setId(input.getId())
                .setProductId(input.getProductId())
                .setSkuCode(input.getSkuCode())
                .setPrice(input.getPrice())
                .setStock(input.getStock())
                .setLowStock(input.getLowStock())
                .setSp1(input.getSp1())
                .setSp2(input.getSp2())
                .setSp3(input.getSp3())
                .setPic(input.getPic())
                .setSale(input.getSale())
                .setPromotionPrice(input.getPromotionPrice())
                .setLockStock(input.getLockStock());


        skuMapper.updateById(skuEntity);
    }

    /**
     * 删除sku
     *
     * @param id id
     */
    @Override
    public void delete(long id) {
        skuMapper.deleteById(id);
    }

    /**
     * 根据Id获取sku
     *
     * @param id id
     * @return sku
     */
    @Override
    public SkuEntity getById(long id) {
        return skuMapper.selectById(id);
    }

    /**
     * 根据批量Id获取sku
     *
     * @param batchIds 批量ids
     * @return sku集合
     */
    @Override
    public List<SkuEntity> listByBatchIds(List<Long> batchIds) {
        if (batchIds == null || batchIds.isEmpty()) {
            return new ArrayList<>();
        }
        return skuMapper.selectBatchIds(batchIds);
    }

    /**
     * 根据商品Id获取sku
     *
     * @param productId 批量ids
     * @return sku集合
     */
    @Override
    public List<SkuEntity> listByProductId(long productId) {
        Assert.isTrue(productId > 0, "商品id不能为空。");

        LambdaQueryWrapper<SkuEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SkuEntity::getProductId, productId);

        return skuMapper.selectList(queryWrapper);
    }

    /**
     * 分页查询
     *
     * @param input input
     * @return 分页的sku
     */
    @Override
    public Page<SkuEntity> pagedQuery(PagedQuerySkuInput input) {
        LambdaQueryWrapper<SkuEntity> queryWrapper = Wrappers.lambdaQuery();

        PageHelper.startPage(input.getPageIndex(), input.getPageSize());

        return (Page<SkuEntity>) skuMapper.selectList(queryWrapper);
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param skuEntity 实体
     * @return sku
     */
    @Override
    public Sku entityToDomain(SkuEntity skuEntity) {
        if (skuEntity == null) {
            return null;
        }

        Sku sku = new Sku();
        sku.setId(skuEntity.getId())
                .setProductId(skuEntity.getProductId())
                .setSkuCode(skuEntity.getSkuCode())
                .setPrice(skuEntity.getPrice())
                .setStock(skuEntity.getStock())
                .setLowStock(skuEntity.getLowStock())
                .setSp1(skuEntity.getSp1())
                .setSp2(skuEntity.getSp2())
                .setSp3(skuEntity.getSp3())
                .setPic(skuEntity.getPic())
                .setSale(skuEntity.getSale())
                .setPromotionPrice(skuEntity.getPromotionPrice())
                .setLockStock(skuEntity.getLockStock());

        return sku;
    }

    /**
     * 将实体对象转换成domain对象
     *
     * @param skuEntities 实体
     * @return skus
     */
    @Override
    public List<Sku> entityToDomain(List<SkuEntity> skuEntities) {
        List<Sku> result = new ArrayList<>();
        if (skuEntities == null) {
            return result;
        }

        for (SkuEntity skuEntity : skuEntities) {
            Sku sku = entityToDomain(skuEntity);
            result.add(sku);
        }
        return result;
    }

}
